package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.*;
import com.qf.pojo.*;
import com.qf.service.DtsGoodsAttributeService;
import com.qf.service.DtsGoodsProductService;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsGoodsSpecificationService;
import com.qf.util.AdminResponseUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {
    @Autowired
    DtsGoodsMapper dtsGoodsMapper;
    @Autowired
    DtsGoodsAttributeMapper attributeMapper;
    @Autowired
    DtsGoodsSpecificationMapper specificationMapper;
    @Autowired
    DtsGoodsProductMapper productMapper;
    @Autowired
    DtsCategoryMapper categoryMapper;
    @Override
    public Integer findCount() {
        Integer count = dtsGoodsMapper.selectCount(null);
        return count;
    }

    @Override
    public IPage<DtsGoods> findByPage(Integer page, Integer limit, String sort, String order, String goodsSn, String name, List<Integer> brands) {
        IPage<DtsGoods> iPage = new Page<>(page,limit);
        QueryWrapper<DtsGoods> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(goodsSn)){
            qw.like("goods_sn",goodsSn);
        }
        if(!StringUtils.isEmpty(name)){
            qw.like("name",name);
        }

        if(!CollectionUtils.isEmpty(brands)){
            qw.in("brand_id",brands);
        }

        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }

        dtsGoodsMapper.selectPage(iPage,qw);

        return iPage;
    }

    @Override
    public Map<String, Object> findByDetail(Integer id) {

        DtsGoods dtsGoods = dtsGoodsMapper.selectById(id);

        LambdaQueryWrapper<DtsGoodsAttribute> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(DtsGoodsAttribute::getGoodsId,id);
        qw1.eq(DtsGoodsAttribute::getDeleted,false);
        List<DtsGoodsAttribute> dtsGoodsAttributes = attributeMapper.selectList(qw1);


        LambdaQueryWrapper<DtsGoodsSpecification> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(DtsGoodsSpecification::getGoodsId,id);
        qw2.eq(DtsGoodsSpecification::getDeleted,false);
        List<DtsGoodsSpecification> specifications = specificationMapper.selectList(qw2);


        LambdaQueryWrapper<DtsGoodsProduct> qw3 = new LambdaQueryWrapper<>();
        qw3.eq(DtsGoodsProduct::getGoodsId,id);
        qw3.eq(DtsGoodsProduct::getDeleted,false);
        List<DtsGoodsProduct> products = productMapper.selectList(qw3);


        Integer lv2 = dtsGoods.getCategoryId();
        DtsCategory dtsCategory = categoryMapper.selectById(lv2);
        Integer lv1 = dtsCategory.getPid();
        Map<String,Object> map = new HashMap<>();
        map.put("categoryIds", Arrays.asList(lv1,lv2));
        map.put("goods",dtsGoods);
        map.put("attributes",dtsGoodsAttributes);
        map.put("specifications",specifications);
        map.put("products",products);
        return map;
    }

    @Autowired
    DtsBrandMapper brandMapper;
    public Object judge(GoodsAllinone goodsAllinone){
        DtsGoods goods = goodsAllinone.getGoods();
        if(StringUtils.isEmpty(goods.getName())){
            return AdminResponseUtil.fail(-1,"商品名称不能为空");
        }

        if(StringUtils.isEmpty(goods.getGoodsSn())){
            return AdminResponseUtil.fail(-1,"商品编号不能为空");
        }
        Integer brandId = goods.getBrandId();
        DtsBrand dtsBrand = brandMapper.selectById(brandId);
        if(dtsBrand==null){
            return AdminResponseUtil.fail(-1,"品牌商为空！");
        }
        Integer categoryId = goods.getCategoryId();
        DtsCategory dtsCategory = categoryMapper.selectById(categoryId);
        if(dtsCategory==null){
            return AdminResponseUtil.fail(-1,"分类为空！");
        }
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        if(attributes!=null&&attributes.length==0){
            return AdminResponseUtil.fail(-1,"商品属性数据为空！");
        }

        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        if(specifications!=null&&specifications.length==0){
            return AdminResponseUtil.fail(-1,"商品规格数据为空！");
        }

        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        if(products!=null&&products.length==0){
            return AdminResponseUtil.fail(-1,"商品库存数据为空！");
        }

        return null;
    }

    @Autowired
    DtsGoodsProductService productService;
    @Autowired
    DtsGoodsSpecificationService specificationService;
    @Autowired
    DtsGoodsAttributeService attributeService;

    @Override
    public Object update(GoodsAllinone goodsAllinone) {
        Object result =this.judge(goodsAllinone);
        if(result!=null){
            return result;
        }
        DtsGoods goods = goodsAllinone.getGoods();
        dtsGoodsMapper.updateById(goods);

        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        productService.updateProduct(goods.getId(),products);


        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        specificationService.updateSpecification(goods.getId(),specifications);


        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        attributeService.updateAttribute(goods.getId(),attributes);


        return ResponseUtil.ok();
    }

    @Override
    public Object delete(DtsGoods dtsGoods) {
        Long count =dtsGoodsMapper.findById(dtsGoods);
        if(count>0){
            return ResponseUtil.fail(-1,"商品正在出售");
        }
        dtsGoods.setDeleted(true);
        dtsGoodsMapper.updateById(dtsGoods);


        attributeService.deleteById(dtsGoods.getId());


        productService.deleteById(dtsGoods.getId());

        specificationService.deleteById(dtsGoods.getId());
        return ResponseUtil.ok();
    }

    @Override
    public Object create(GoodsAllinone goodsAllinone) {
        Object judge = this.judge(goodsAllinone);
        if(judge!=null){
            return judge;
        }

        DtsGoods goods = goodsAllinone.getGoods();
        LambdaQueryWrapper<DtsGoods> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoods::getName,goods.getName());
        Integer count = dtsGoodsMapper.selectCount(qw);
        if(count>0){
            return ResponseUtil.fail(-1,"商品名称已存在");
        }

        goods.setDeleted(true);
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        dtsGoodsMapper.insert(goods);

        attributeService.addAttribute(goods.getId(),goodsAllinone.getAttributes());

        specificationService.addspecification(goods.getId(),goodsAllinone.getSpecifications());


        productService.addproduct(goods.getId(),goodsAllinone.getProducts());
        return ResponseUtil.ok();
    }
}
